package com.orangelife.mobile.annoucement.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.curry.android.http.VolleyHttpUtil;
import com.curry.orangelife.mobile.R;
import com.orangelife.mobile.annoucement.biz.MessageHttpBiz;
import com.orangelife.mobile.common.activity.OrangeLifeBaseActivity;
import com.common.jump.MessageJumper;
import com.orangelife.mobile.common.view.EmptyLayout;
import com.orangelife.mobile.util.StringUtil;
import com.orangelife.mobile.web.WebWin;

import org.json.JSONObject;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 公告详情
 * <p/>
 * Created by yyx on 2015/11/11.Modify by wgf on 160701.
 *
 * @author yyx
 */
public class AnnoucementDetailActivity extends OrangeLifeBaseActivity implements View.OnClickListener {

    @Bind(R.id.ib_title_left)
    LinearLayout ibTitleLeft;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.wwDes)
    WebWin wwDes;
    @Bind(R.id.llRightClick)
    LinearLayout llRightClick;
    @Bind(R.id.iv_title_right)
    ImageView ivTitleRight;
    @Bind(R.id.emptyLayout)
    EmptyLayout emptyLayout;
    private String url = null;
    private boolean isMore;
    private String mIsRead = "1";
    private boolean mHasGetNewUrl;
    private String annoID;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_annoucement_detail);
        ButterKnife.bind(this);
        getIntentInfo();
        initView();
        loadUrl();
        setListener();
    }

    private void getIntentInfo() {
        Intent intent = getIntent();
        url = intent.getStringExtra("url");
        isMore = intent.getBooleanExtra("isMore", true);
        mIsRead = intent.getStringExtra("isRead");
        if ("0".equals(mIsRead)) {
            annoID = intent.getStringExtra("annoID");
        }
    }

    private void initView() {
        tvTitle.setText("公告详情");
        llRightClick.setOnClickListener(this);
        if (isMore) {
            ivTitleRight.setVisibility(View.VISIBLE);
        } else {
            ivTitleRight.setVisibility(View.GONE);
        }
    }

    private void setListener() {
        emptyLayout.setOnLayoutClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                loadUrl();
            }
        });
    }

    private void requestAnnoDetail() {
        MessageHttpBiz.requestDetail(annoID, "1", new VolleyHttpUtil.ResponseCallBack() {
            @Override
            public void handleResponse(JSONObject response, int errCode) {
                url = MessageHttpBiz.handleOrderDetail(response);
                if (!StringUtil.isBlank(url)) {
                    mHasGetNewUrl = true;
                    loadUrl();
                } else {
                    emptyLayout.dismiss();
                    emptyLayout.setErrorType(EmptyLayout.NODATA);
                }
            }
        });
    }

    private void loadUrl() {
        if ("0".equals(mIsRead) && !mHasGetNewUrl) {
            requestAnnoDetail();
        } else {
            wwDes.setWebViewClient(new MyWebViewClient());
            wwDes.getSettings().setJavaScriptEnabled(true);
            wwDes.loadUrl(url);
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.llRightClick:
                MessageJumper.jumpToAnnouncementList(AnnoucementDetailActivity.this);
                finish();
                break;
        }
    }

    class MyWebViewClient extends WebViewClient {

        public MyWebViewClient() {
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            return true;
        }

        @Override
        public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
            super.onReceivedError(view, request, error);
            emptyLayout.dismiss();
            emptyLayout.setErrorType(EmptyLayout.NETWORK_ERROR);
        }

        /**
         * 要等js全部加载完毕后，才能过调用js函数
         */
        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            emptyLayout.dismiss();
        }
    }
}
